﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace CodeGenerator
{
    public partial class frmWelcome : DockContent
    {
        public frmWelcome()
        {
            InitializeComponent();
            base.CloseButtonVisible = false;
        }

        private void frmWelcome_Load(object sender, EventArgs e)
        {
            string[] strArray = Directory.GetFiles(Program.errorpath, "*.log", SearchOption.TopDirectoryOnly);
            List<Filepath> list = new List<Filepath>();
            foreach (string str in strArray)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    list.Add(new Filepath(str));
                }
            }
            this.cbmerrorlist.Items.AddRange(list.ToArray());
            this.cbmerrorlist.SelectedIndex = this.cbmerrorlist.Items.Count - 1;
            Geterror();
        }
        private void Geterror()
        {
            if (this.cbmerrorlist.SelectedIndex == -1)
            {
                this.txtLog.Text = string.Empty;
            }
            else
            {
                this.txtLog.Text = File.ReadAllText(Path.Combine(Program.errorpath, ((Filepath)this.cbmerrorlist.SelectedItem).FilePath));
            }
        }
        public class Filepath
        {
            public string FileName;
            public string FilePath;

            public Filepath(string file)
            {
                this.FilePath = file;
                this.FileName = file.Substring(file.LastIndexOf('\\') + 1);
            }

            public override string ToString() =>
                this.FileName;
        }
        private void cbmerrorlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            Geterror();
        }
    }
}
